package com.xinyiweidai.xinyiweidai.util;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;

import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;


/**
 * Data：2018/10/26-13:20
 * Author: lanxiong
 * Description:
 */

public class LoginUtil {

    public static String getMacAddr() {
        try {
            List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface nif : all) {
                if (!nif.getName().equalsIgnoreCase("wlan0"))
                    continue;

                byte[] macBytes = nif.getHardwareAddress();
                if (macBytes == null) {
                    return "";
                }

                StringBuilder res1 = new StringBuilder();
                for (byte b : macBytes) {
                    res1.append(String.format("%02X:", b));
                }

                if (res1.length() > 0) {
                    res1.deleteCharAt(res1.length() - 1);
                }
                return res1.toString();
            }
        } catch (Exception ex) {
        }
        return "02:00:00:00:00:00";
    }


    // 纬度
    public static double latitude = 0.0;
    // 经度
    public static double longitude = 0.0;


    public static void initLocation(Context context) {
        String locationProvider = null;
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        List<String> providers = null;

        if (locationManager != null) {
            providers = locationManager.getProviders(true);
        }

        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

                    if (providers != null) {
                        if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
                            //如果是Network
                            locationProvider = LocationManager.NETWORK_PROVIDER;
                        } else if (providers.contains(LocationManager.GPS_PROVIDER)) {
                            //如果是Gps
                            locationProvider = LocationManager.GPS_PROVIDER;
                        } else {
                            Intent i = new Intent();
                            i.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            context.startActivity(i);
                        }
                    }

                    Location location = null;
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(locationProvider);
                    }
                    if (location != null) {
                        latitude = location.getLatitude(); // 经度
                        longitude = location.getLongitude(); // 纬度
                    }
                }
    }
}
